package com.sample.transitiondemo;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;

import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by gouzhun on 2016/4/18.
 */
public class DrawUtils {
    private static final String TAG = "DrawUtils";


    private static HashMap<String, Bitmap> maskMap;

    /**
     * 获取一个bitmap，目的是用来承载drawable;
     * <p/>
     * 将这个bitmap放在canvas上面承载，并在其上面画一个图形来固定显示区域
     *
     * @param pathStr
     * @param width
     * @param height
     * @return
     */
    public static Bitmap createDiamondBitmap(String pathStr, final int width, final int height, boolean isRoundCorner) {
        if (maskMap == null) {
            maskMap = new HashMap<>();
        }

        if (maskMap.containsKey(pathStr)) {
            Bitmap lb = maskMap.get(pathStr);
            if (lb != null && !lb.isRecycled()) {
                return maskMap.get(pathStr);
            }
        }
        int destWidth, destHeight;
        if (width == 0 || height == 0) {
            destWidth = destHeight = 8;
        } else {
            destWidth = width;
            destHeight = height;
        }

        Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
        Bitmap localBitmap = Bitmap.createBitmap(destWidth, destHeight, localConfig);
        Canvas localCanvas = new Canvas(localBitmap);
        Paint localPaint = new Paint();
        localPaint.setStyle(Paint.Style.FILL);
        Path shapePath = createPath(destWidth, destHeight, pathStr);

        if (isRoundCorner) {
            localPaint.setPathEffect(new CornerPathEffect(8));
        }

        localCanvas.drawPath(shapePath, localPaint);
        maskMap.put(pathStr, localBitmap);
        return localBitmap;
    }


    public static Path createPath(int width, int height, String ptStr) {
        Path path = new Path();
        String[] ptsStr = ptStr.split(",");
        float x, y;
        for (int i = 0; i < ptsStr.length; i++) {
            if (i % 2 == 0) {
                x = Float.parseFloat(ptsStr[i]) * width;
                y = Float.parseFloat(ptsStr[i + 1]) * height;
                //ContentUtil.log(TAG, x + "," + y + "    " + Float.parseFloat(ptsStr[i]) + "," + Float.parseFloat(ptsStr[i + 1]) + "    " + width + "," + height + "       " + ptsStr[i] + "," + ptsStr[i + 1]);
                if (i == 0) {
                    path.moveTo(x, y);
                } else {
                    path.lineTo(x, y);
                }
            }
        }

        path.close();
        return path;
    }


   /* public static Bitmap getMySizeBitmap(Context context, int drawableId, int destWidth, int destHeight) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
        return resizeBitmap(destWidth, destHeight, bitmap);
    }*/

    public static Bitmap resizeBitmap(float destWidth, float destHeight, Bitmap bitmap) {
        Bitmap defaultBitmap = null;
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(destWidth / width, destHeight / height);
            defaultBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            return defaultBitmap;
        }
        //bitmap.recycle();
        return defaultBitmap;
    }

    public static Bitmap getMySizeBitmap(Context context, int resId, int destWidth, int destHeight) {
        // 首先设置 inJustDecodeBounds=true 来获取图片尺寸
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Resources res = context.getResources();
        BitmapFactory.decodeResource(res, resId, options);

        // 计算 inSampleSize 的值
        options.inSampleSize = calculateInSampleSize(options, destWidth, destHeight);

        // 根据计算出的 inSampleSize 来解码图片生成Bitmap
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeResource(res, resId, options);
        return resizeBitmap(destWidth, destHeight, bitmap);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // 原始图片的宽高
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // 在保证解析出的bitmap宽高分别大于目标尺寸宽高的前提下，取可能的inSampleSize的最大值
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public static Bitmap getMySizeBitmap(InputStream is, int destWidth, int destHeight) {
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        return resizeBitmap(destWidth, destHeight, bitmap);
    }

}
